<?php
	include 'ZionHeader.inc';
?>
<HTML>
<head>
<title>ZionsAuto</title>

	<?php
		$name = $_SESSION['UName'];
		$password = $_POST['password'];
		
		$submit = $_POST["Search"];
		$searchQ  = $_POST['searchQ'];
		$price = $_POST['Price'];
		$sortBy = $_POST['Sort'];
		//echo $sortBy;
		
		//Need to compare $name with username of announcement
	?>

  <link rel="stylesheet" href="styles.css" type="text/css" />
</head>
<BODY>
<h1><center>Search</center></h1>

<!-- Tab Menu Goes here -->
<div id="tabs">
  <ul>
    <li><a href="homePage.php"><span>Home</span></a></li>
	<li><a href="announcement.php"><span>Announcement</span></a></li>
	<?php
	$AccountType = $_SESSION['AType'];
	if ($AccountType == 'R')
	{
		echo "<li><a href='viewBookmark.php'><span>View Bookmarks</span></a></li>";
		echo "<li><a href='editProfile.php'><span>Edit Profile</span></a></li>";
	}
	
	if ($AccountType == 'A')
	{
		echo "<li><a href='statistics.php'><span>View statistics</span></a></li>";
	}
	?>
    <li><a href="logout.php"><span>Log Out</span></a></li>
  </ul>
</div>

<p> Enter Your Search Terms</p>

Search by Make, Model or Year
<form action='Browse.php' method = "Post">
<?php
    echo "<input type='text' name='searchQ' ";
	if ($searchQ != ''){ echo "value=".$searchQ." "; }
	echo "/></br>";
?>
Search by Price Range	
	<select name="Price">
	<option value="5000" <?php if ($price == 5000){ echo "selected ";}  ?> > <5000 </option>
	<option value="10000" <?php if ($price == 10000){ echo "selected ";}  ?> >10000-15000</option>
	<option value="15000" <?php if ($price == 15000){ echo "selected ";}  ?> >15000-20000</option>
	<option value="20000" <?php if ($price == 20000){ echo "selected ";}  ?> >20000-25000</option>
	<option value="25000" <?php if ($price == 25000){ echo "selected ";}  ?> >25000-30000</option>
	<option value="30001" <?php if ($price == 30001){ echo "selected ";}  ?> > >30000</option>
	</select></br>
Sort By
	<input type='radio' name = 'Sort'  value = 'Sprice' <?php if ($sortBy == "Sprice"){ echo "checked ";}  ?> /> Price
	<input type='radio' name = 'Sort' value = 'Smake' <?php if ($sortBy == "Smake" || $sortBy == ""){ echo "checked ";}  ?> /> Make and Model
	<input type='radio' name = 'Sort' value = 'Sid' <?php if ($sortBy == "Sid"){ echo "checked ";}  ?> /> SellerId
	<input type='radio' name = 'Sort' value = 'Stime' <?php if ($sortBy == "Stime"){ echo "checked ";}  ?> /> Timestamp </br>
	<input type='submit' name='Search' value ='Search'/>
</form>


<div>
<?php
	
	
	
	if ($submit == "Search")
	{
		echo " Search Term Entered: ";
		echo $search;
		//echo $model;
		//echo $price;
		echo $year;
		
		$AccountType = $_SESSION['AType'];
		include 'DatabaseConnect.inc';
		
		/*if ($AccountType == 'D'){
		$queryAnn2 = "SELECT * FROM Announcement where userID = '$name'";
		$resultAnn2 = mysql_query($queryAnn2);
		$rowAnn2 = mysql_fetch_array($resultAnn2);
		}*/
		
		if ($searchQ != ''){
			$query = "SELECT * FROM Vehicle WHERE (Make ='$searchQ' or Model ='$searchQ' or Year ='$searchQ' )";	}
		else if ($price != '')	{
			$priceTop = $price+5000; 
			if ($price == 5000)
			{	echo "Price < ".$price;
				$query = "SELECT * FROM Vehicle WHERE Price < $price+1 ";	}
			else if ($price == 30001)
			{	echo "Price > ".$price;
				$query = "SELECT * FROM Vehicle WHERE Price > ($price-1)";	}
			else
			{	echo $priceBot." < Price < ".$price;
				$query = "SELECT * FROM Vehicle WHERE (($price) < Price and Price < $priceTop+1)";	}
		}
			
		/*if ($AccountType == 'D'){
			$queryFinal = $query."and  ID = '".$rowAnn2['vehicle_ID']."'";
		}*/
				
		echo "</br>";
		echo $queryFinal;
		// if sort option just have it append appropriate sort by clause

		echo "<table border='1'>";
		echo "<tr>  <th>Make</th> <th>Model</th> <th>Year</th> <th>Price</th> <th>Vin</th> <th>Seller ID</th> <th>Timestamp</th> <th>Photo</th>";
		
		echo "</tr>";
		
		// keeps getting the next row until there are no more to get
		//echo $sortBy;
		if ($sortBy == "Smake" or $sortBy == "Sprice")
		{
			if ($sortBy == "Smake")
			{
				$queryFinal =  $query." ORDER BY Make, Model";
			}
			else if ($sortBy == "Sprice")
			{
				$queryFinal =  $query." ORDER BY Price";
			}	
			$result = mysql_query($queryFinal);
			
			while($row = mysql_fetch_array( $result, MYSQL_ASSOC ))
			{
				// Print out the contents of each row into a table
				$queryAnn = "SELECT * FROM Announcement Where vehicle_ID = $row[ID]";
				if ($AccountType == 'D'){
				$queryAnn = $queryAnn." and userID = '$name'";}
				
				$resultAnn = mysql_query($queryAnn);
				$rowAnn = mysql_fetch_array($resultAnn);
				//echo $row['ID'];

				if( $row['ID'] == $rowAnn['vehicle_ID'])
				{
					echo "<tr><td>"; 
					echo $row['Make'];
					echo "</td><td>"; 
					echo $row['Model'];
					echo "</td><td>"; 
					echo $row['Year'];
					echo "</td><td>"; 
					echo $row['Price'];
					echo "</td><td>"; 
					echo $row['VIN'];
					echo "</td><td>"; 
					//echo sellerID;//display the seller ID and timestamp
					echo $rowAnn['userID'];
					echo "</td><td>"; 
					echo $rowAnn['Timestamp'];//display the seller ID zand timestamp
					echo "</td><td>";
					echo $row['Photo'];
					echo "<img src='viewimage.php?id=".$row['ID']."' width=250 height=200>";
					//printf("<a href="http://www.cse.msu.edu/~sunxiaok/cgi-bin/chrysler300c.jpg">%s</a></br>",$row['Photo']);;
					echo "</td></tr>"; 
				}
			}
		} 	
		else{
			$queryAnn = "SELECT * FROM Announcement ";
			
			//$result1 = mysql_query($query);
			//$row1 = mysql_fetch_array($result1);
			
			//$queryAnn = $queryAnn." WHERE vehicle_ID = '".$row1['ID']."' ";
			
			if ($AccountType == 'D'){
				$queryAnn = $queryAnn." Where userID = '$name' ";
			}
			if ($sortBy == "Sid"){
				$queryAnnFin =  $queryAnn." ORDER BY userID ASC";
			}
			else if ($sortBy == "Stime"){
				$queryAnnFin =  $queryAnn." ORDER BY Timestamp ASC";
			}
			else
			{   $queryAnnFin = $queryAnn;}
			
			$resultAnn = mysql_query($queryAnnFin);
		while($rowAnn = mysql_fetch_array( $resultAnn, MYSQL_ASSOC ))
		{
			if ($searchQ != ''){
				$query = "SELECT * FROM Vehicle WHERE (Make ='$searchQ' or Model ='$searchQ' or Year ='$searchQ' )  ";	}
			else if ($price != '')	{
				$priceTop = $price+5000;
				if ($price == 5000)
				{	//echo "Price < ".$price;
					$query = "SELECT * FROM Vehicle WHERE Price < $price+1 ";	}
				else if ($price == 30001)
				{	//echo "Price > ".$price;
					$query = "SELECT * FROM Vehicle WHERE Price > ($price-1)";	}
				else
				{	//echo $priceBot." < Price < ".$price;
					$query = "SELECT * FROM Vehicle WHERE (($price) < Price and Price < $priceTop+1)";	}
			}
		
			$rowGrab = $rowAnn['vehicle_ID'];
			//echo $rowGrab;
			$queryFinal = $query." and ID = '$rowGrab'";
			//echo $queryFinal;
			
			//echo $rowAnn['userID'];
				
			/*if ($AccountType == 'D'){
			$queryFinal = $queryFinal."and ID = '".$rowAnn['vehicle_ID']."'";
			}*/
			$result = mysql_query($queryFinal);
			$row = mysql_fetch_array($result);
			////echo "<br>vehicle table".$row['ID'];
			//echo "<br>Ann table".$rowAnn['vehicle_ID'];
			if( $row['ID'] == $rowAnn['vehicle_ID'])
			{
				echo "<tr><td>"; 
				echo $row['Make'];
				echo "</td><td>"; 
				echo $row['Model'];
				echo "</td><td>"; 
				echo $row['Year'];
				echo "</td><td>"; 
				echo $row['Price'];
				echo "</td><td>"; 
				echo $row['VIN'];
				echo "</td><td>"; 
				//echo sellerID;//display the seller ID and timestamp
				echo $rowAnn['userID'];
				echo "</td><td>"; 
				echo $rowAnn['Timestamp'];//display the seller ID zand timestamp
				echo "</td><td>";
				echo $row['Photo'];
				echo "<img src='viewimage.php?id=".$row['ID']."' width=250 height=200>";
				//printf("<a href="http://www.cse.msu.edu/~sunxiaok/cgi-bin/chrysler300c.jpg">%s</a></br>",$row['Photo']);;
				echo "</td></tr>"; 
			}
		}
		}

		echo "</table>";

		
		mysql_close();
	}
?>
</div>


<!-- Footer Goes Here -->
<div id="footer">
<hr>
<P>(C)Copyright 2011 Zion's DBA. All Rights Reserved</P>
</div>

</BODY>
</HTML>